package messagesvc.executor.template;


import cn.hutool.core.util.RandomUtil;
import cn.ucloud.microsf.commonlib.enums.ResponseCode;
import cn.ucloud.microsf.commonlib.response.EmptyResponse;
import cn.ucloud.microsf.commonlib.util.Utils;
import cn.ucloud.microsf.messagesvc.orm.entity.TemplateEntity;
import cn.ucloud.microsf.messagesvc.orm.mapper.TemplateMapper;
import cn.ucloud.microsf.messagesvc.request.template.TemplateCreateRequest;
import cn.ucloud.microsf.mpcsvc.common.enums.DataStatus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 消息模板
 */
@Slf4j
@Service
public class TemplateCreateExecutor {

  @Autowired
  private TemplateMapper templateMapper;

  public EmptyResponse execute(TemplateCreateRequest request) {

    TemplateEntity entity = new TemplateEntity();
    entity.setUuid(Utils.generateUUID());
    entity.setCode(RandomUtil.randomStringUpper(8));
    entity.setName(request.getName());
    entity.setContent(request.getContent());
    entity.setType(request.getType());
    entity.setIsWide(request.getIsWide());
    entity.setStatus(DataStatus.VALID);

    templateMapper.insert(entity);

    return EmptyResponse.builder()
        .code(ResponseCode.CODE_SUCCESS)
        .msg(ResponseCode.CODE_SUCCESS.getDefaultMessage())
        .build();

  }

}

